import { queryPage, queryInfoById, create, remove, update, queryMenuTree, queryAuthById, setAuthByIdChecked } from './service';
import { routerRedux } from 'dva/router';

function setList(list, index) {
  list[index].status = list[index].status === 'lock' ? 'normal' : 'lock';
  return list;
}

let Res, parentKeyMap = {};

function getTree(data) {
  let result = {};
  data.forEach(v => {
    v.parentId = v.parentId || 'ROOT';
    if (!result[v.parentId]) {
      result[v.parentId] = [];
    }
    result[v.parentId].push({
      title: v.name,
      key: v.id.toString(),
      parentId: v.parentId,
      depth: v.depth
    });
    parentKeyMap[v.id.toString()] = v.parentId === 'ROOT' ? '' : v.parentId;
  });
  Res = result;
  let tree = [];
  getTreeNode(result['ROOT'], tree);

  return tree;
  // this.setState({ treeData: tree });
}

function getTreeNode(arr, children) {
  arr.forEach(a => {
    if (Res[a.key]) {
      a.children = [];
      children.push(a);
      getTreeNode(Res[a.key], a.children);
    } else {
      children.push(a);
    }
  });
}

export default {
  namespace: 'role',

  state: {
    list: { data: [] },
    details: null,
    cond: {}
  },

  effects: {
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: {} });
      yield put({ type: 'queryPage' })
    },
    *updateCond({ payload }, { put }) {
      yield put({ type: 'setCond', payload });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1, pageSize: 10 } }, { call, put, select }) {
      const cond = yield select(state => state.role.cond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setPage', payload: { list, total } })
      // return list
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryEditInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.id);
      const items = {};
      payload.keys.forEach(item => {
        items[item] = response[item]
      })
      yield put({ type: 'setEditInfo', payload: items })
    },
    *create({ payload }, { call, put }) {
      yield call(create, payload);
      yield put(routerRedux.push('/data/role'));
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.push('/data/role'));
    },
    *remove({ payload }, { call, put }) {
      yield call(remove, payload)
    },
    *queryAuthById({ payload }, { all, call, put, select }) {
      const details = yield select(state => state.role.details);

      if (!details) {
        yield put({ type: 'queryInfoById', payload })
      }

      const [allMenu, checkMenu] = yield all([
        call(queryMenuTree, payload),
        call(queryAuthById, payload)
      ]);
      const tree = getTree(allMenu);
      const checked = checkMenu.map(v => v.resourceId);
      yield put({ type: 'setRoleTree', payload: { tree, checked, parentKeyMap } })
    },
    *submitAuth(_, { call, select, put }) {
      const details = yield select(state => state.role.details);
      const checked = yield select(state => state.role.checked);
      yield call(setAuthByIdChecked, details.id, checked);
      yield put(routerRedux.push('/data/role'));
    }
  },

  reducers: {
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    },
    setOneLock(state, action) {
      return {
        ...state,
        list: {
          ...state.list,
          data: setList(state.list.data, action.payload)
        }
      }
    },
    setRoleTree(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },
    setChecked(state, action) {
      return {
        ...state,
        checked: action.payload
      }
    }
  }
}
